<template>
  <!-- 日志卡片 -->
  <div class="daily-card">
    <!-- 完整卡片 -->
    <div class="card" v-for="item in cardItems" :key="item.d_id" @click="goDailyDetail(item)">
      <!-- 卡片顶部 -->
      <div class="card-word">
        <div class="card-word-left">
          <!-- 头像 -->
          <img class="user-pic" :src="item.d_img" alt />
          <!-- 伴宠人员姓名 -->
          <span class="user-name">{{item.d_man_name}}</span>
        </div>
        <!-- 发布城市 -->
        <span class="user-city">{{item.d_city}}</span>
      </div>
      <!-- 发布内容 -->
      <div class="card-img">
        <!-- 文字 -->
        <p>{{item.d_detail}}</p>
        <!-- 照片 -->
        <img :src="item.d_img" alt />
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: ["type"], // type:all-查所有,three-查三条
  data() {
    return {
      cardItems: [] // 存放日志列表
    };
  },
  async mounted() {
    await this.getDairyData();
  },
  methods: {
    // 获取三条最新日志信息
    getDairyData() {
      let url = this.$api.getThreeDairy;
      if (this.type == "all") {
        url = this.$api.getDairyAll;
      }
      this.http
        .get(url)
        .then(res => res.data)
        .then(res => {
          console.log("返回最新日志：", res.data);
          this.cardItems = res?.data;
        });
    },
    // 跳转到日志详情
    goDailyDetail(item) {
      this.$router.push({
        path: "/dailydetail",
        query: {
          d_id: item.d_id
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.card {
  color: #36343c;
  background-color: #fff;
  width: 90%;
  height: 30rem;
  margin: 2rem auto;
  border-radius: 1rem;
  box-shadow: 0 0.4rem 0.8rem 0 rgba(0, 0, 0, 0.2),
    0 0.6rem 2rem 0 rgba(0, 0, 0, 0.19);

  .card-word {
    clear: both;
    width: 90%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin: auto;
    padding-top: 2rem;

    .card-word-left {
      width: 25rem;
      display: flex;
      justify-content: flex-start;
      align-items: center;

      .user-pic {
        display: block;
        width: 3rem;
        height: 3rem;
        /*剪裁掉多余部分，不拉伸，不失真*/
        object-fit: cover;
        border-radius: 100%;
        background-color: #fff;
      }

      .user-name {
        margin-left: 1.5rem;
        display: inline-block;
        font-weight: bold;
        font-size: 1.6rem;
      }
    }

    .user-city {
      color: #36343c;
    }
  }

  .card-img {
    width: 90%;
    height: 90%;
    margin: 0 auto;

    p {
      margin-top: 1rem;
      font-size: 1.5rem;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      text-align: start;
    }

    img {
      margin-top: 1rem;
      width: 100%;
      height: 70%;
      border-radius: 1rem;
      object-fit: cover;
    }
  }
}
</style>
